<template>
	<view class="container_135">
		<view class="top_image">
			<image src="/static/clb/135/top.png" style="width: 750rpx" mode="widthFix"></image>
		</view>
		<view class="form-container">
			<uni-forms class="login-form" ref="accountForm" :model="formData">
				<view class="first-gif" style="text-align: center" v-if="showFirst">
					<image src="/static/clb/132/first.gif" style="width: 400rpx" mode="widthFix"></image>
				</view>
				<uni-forms-item class="input-phone">
					<input
						placeholder="请输入手机号"
						type="tel"
						maxlength="11"
						style="text-align: center; font-size: 36rpx"
						v-model="formData.mobile"
						class="uni-input-input"
						placeholder-style="color: #969696"
					/>
				</uni-forms-item>
				<uni-forms-item style="margin-top: 40rpx">
					<view class="send-code">
						<input
							style="width: 350rpx; margin-left: 50rpx; text-align: center; font-size: 36rpx"
							maxlength="6"
							type="tel"
							v-model="formData.smsCode"
							placeholder="请输入验证码"
							class="uni-input-input"
							placeholder-style="color: #969696"
						/>
						<view class="code-line"></view>
						<view class="code" :style="{ cursor: isSend ? 'not-allowed' : '' }" @click="sendCode(true)">
							{{ isSend ? `重新发送 ${isNum}` : '获取验证码' }}
						</view>
					</view>
				</uni-forms-item>
				<view class="login-btn" style="text-align: center">
					<image src="/static/clb/135/btn.png" mode="" style="width: 516rpx; height: 115rpx" @click="confirmOrder"></image>
				</view>

				<view style="margin-top: 40rpx; margin-bottom: 0rpx; padding-bottom: 0rpx; display: flex; align-items: center; justify-content: center">
					<checkbox-group @change="changeAll">
						<label>
							<checkbox value="cb" :checked="allChecked" style="transform: scale(0.7)" />
							<text style="color: #fdfdfd; font-weight: 400; font-size: 26rpx">已阅读并同意</text>
						</label>
					</checkbox-group>
					<text style="color: #ffd700; font-weight: 400; font-size: 28rpx" @click="privatePro(135)">《活动规则》</text>
					<text style="color: #ffd700; font-weight: 400; font-size: 28rpx" @click="privatePro(1)">《隐私条款》</text>
				</view>
			</uni-forms>
		</view>
		<view class="bottom_image">
			<image src="/static/clb/135/bottom.png" style="width: 750rpx" mode="widthFix"></image>
		</view>
		<!--no 无 二次确认 -->

		<!-- 规则 -->
		<uni-popup ref="popupPro" type="bottom" border-radius="10px 10px 0 0">
			<per-info :agreementType="agreeType" @ok="ok1"></per-info>
		</uni-popup>
	</view>
</template>

<script setup>
import { reactive, ref, onMounted, watchEffect, watch } from 'vue';
import { onLoad, onPageScroll } from '@dcloudio/uni-app';
import initApi from '@/apis/init.js';
import { storeToRefs } from 'pinia';
import { useCounterStore } from '@/store/counter.js';

const { formatPhone } = storeToRefs(useCounterStore());
const agreeType = ref(1);
const popupPro = ref();
const isSend = ref(false);

const showFirst = ref(true);
const isNum = ref(0);
const isTimer = ref();
const showHeight = ref(0);
const accountForm = ref();
const allChecked = ref(false);
const formData = reactive({
	mobile: '',
	smsCode: null,
	orderNo: null,
	channelCode: '',
	productCode: ''
});

onLoad((query) => {
	if (!query.a || !query.p) {
		uni.reLaunch({
			url: '/pages/clb/404'
		});
	}
	if (formatPhone.value) {
		formData.mobile = formatPhone.value;
	}
	formData.channelCode = query.a;
	formData.productCode = query.p;
});
watch(
	() => formData.mobile,
	(newVal, oldValue) => {
		const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(newVal);
		if (isOkMobile) {
			formatPhone.value = newVal;
			uni.hideKeyboard();
			// sendCode(false);
		}
	}
);

const twoConfirmOrder = async () => {
	try {
		const arr = {
			orderNo: formData.orderNo,
			mobile: formData.mobile,
			smsCode: formData.smsCode
		};
		const res = await initApi.processInventoryPackageOrder(arr);
		if (res.code != 20000) {
			window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
			return uni.utils.toast(res.message);
		}
		clearInterval(isTimer.value);
		isNum.value = 0;
		isSend.value = false;
		return uni.utils.toast('订购成功');
	} catch (e) {
		window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
		console.log(e);
		//TODO handle the exception
	}
};

// 二次确认弹框 前期需要判断什么 自己打开吧
const confirmOrder = () => {
	try {
		if (!formData.mobile) return uni.utils.toast('请输入手机号');
		if (!formData.smsCode) return uni.utils.toast('请输入验证码');
		if (!allChecked.value) return uni.utils.toast('请勾选《活动规则》《隐私协议》');
		twoConfirmOrder();
	} catch (e) {
		window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
		//TODO handle the exception
	}
};

const changeAll = (val) => {
	if (val.detail.value.length > 0) {
		allChecked.value = true;
	} else {
		allChecked.value = false;
	}
};
const privatePro = (type) => {
	agreeType.value = type;
	popupPro.value.open('center');
};
const ok1 = () => {
	popupPro.value.close();
	allChecked.value = true;
};

const sendCode = async (flag) => {
	const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(formData.mobile);
	if (!isOkMobile) return uni.utils.toast('请输入正确的手机号');

	if (isSend.value) return;
	// 归宿地判断
	const address = await initApi.ipPhone({ phone: formData.mobile });
	if (address.code == 20000 && address.data.result.province != '上海') {
		window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
		return uni.utils.toast(`输入归属地为上海的手机号码，该手机号码归属地为 ${address.data.result.province}`);
	}
	try {
		const arr = {
			mobile: formData.mobile,
			channelCode: formData.channelCode,
			productCode: formData.productCode
		};

		const res = await initApi.initInventoryPackageOrder(arr);

		if (res.code == 20000) {
			showFirst.value = false;
		} else {
			window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
			return uni.utils.toast(res.message);
		}

		formData.orderNo = res.data.orderNo;
		isNum.value = 60;
		isSend.value = true;
		isTimer.value = setInterval(() => {
			if (isNum.value <= 0) {
				clearInterval(isTimer.value);
				isNum.value = 0;
				isSend.value = false;
			}
			isNum.value--;
		}, 1000);
		return uni.utils.toast('验证码发送成功');
	} catch (e) {
		console.log(e);
		window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
		//TODO handle the exception
		uni.utils.toast('请输入正确手机号');
		isSend.value = false;
		isNum.value = 0;
		clearInterval(isTimer.value);
	}
};
</script>

<style scoped lang="scss">
.container_135 {
	width: 750rpx;
	height: 100%;
	background-color: #f23a47;
	margin: 0 auto;
	box-sizing: border-box;
	overflow-x: hidden;
}
::v-deep {
	image {
		vertical-align: bottom; // 解决image背景图片下方有10rpx 的白边
	}
}
:deep(.uni-checkbox-input) {
	border-radius: 80rpx !important;
}
.confirm {
	.btn-con {
		display: flex;
		justify-content: space-between;
	}
	.btn-close {
		margin-top: 35rpx;
		display: flex;
		justify-content: center;
	}
}
.form-container {
	margin: 0 auto;
	height: 600rpx;
	width: 750rpx;
	background-color: #f23a47;
	.login-btn {
		margin-top: 40rpx;
		animation: scaleUp 2s infinite;
		/* 动画名称、持续时间、是否无限循环 */
	}
	@keyframes scaleUp {
		0%,
		50% {
			transform: scale(1); /* 开始时的缩放比例 */
		}
		25%,
		75% {
			transform: scale(1.05); /* 缩放比例最大值 */
		}
		100% {
			transform: scale(1); /* 结束时的缩放比例 */
		}
	}
	.uni-forms-item {
		height: 100rpx;
		margin: 0 29rpx;
		background-color: #ffffff;
		border-radius: 100rpx;
		box-sizing: border-box;
		width: 692rpx;

		.uni-input-input {
			width: 592rpx;
			margin-left: 50rpx;
		}
	}
}
.send-code {
	display: flex;
	height: 100rpx;
	.code-line {
		height: 50rpx;
		width: 2rpx;
		border-left: 2rpx #e33c50 solid;
		margin-top: 25rpx;
		margin-right: 6rpx;
	}
	.code {
		height: 100rpx;
		width: 240rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #f23a47;
		font-size: 30rpx;
		cursor: pointer;
	}
}
</style>
